package net.ix.DroidLab;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

import android.view.Menu;
import android.view.MenuItem;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class IXDroidLab extends ListActivity {
    /** Called when the activity is first created. */
	public static final int MENU_ITEM_HELLO = Menu.FIRST;
	public static final int MENU_ITEM_ADD = Menu.FIRST+1;
	public static final int MENU_ITEM_INSERT = Menu.FIRST+2;
	
	private String listDataKey[] = {"name"};
	ArrayList<HashMap<String,Object>> listDatas = new ArrayList<HashMap<String,Object>>();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fillData();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	menu.add(0, MENU_ITEM_HELLO, Menu.FIRST, R.string.menu_hello);
    	menu.add(0, MENU_ITEM_ADD, Menu.FIRST+1, R.string.menu_add);
    	menu.add(0, MENU_ITEM_INSERT, Menu.FIRST+2, R.string.menu_insert);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	switch (item.getItemId()) {
    	case MENU_ITEM_HELLO:
    		sayHello();
    		return true;
    	case MENU_ITEM_INSERT:
    		addItem();
    		return true;
    	}
    	return true;
    }
    
    public String[] COUNTRIES = new String[]{
    	"DeathCatEyes",
    	"Ivan",
    	"SunShine",
    	"XX"
    };
    
    private void addItem(){
    	int itemLength = listDatas.size()+1;
    	String itemKey = "name";
    	String itemVal = "Item " + itemLength;
    	
    	HashMap<String,Object> listData = new HashMap<String,Object>();
    	
    	listData.put(itemKey, itemVal);
    	
    	listDatas.add(listData);
    	
    	fillData();
    }
    
    private void fillData(){
    	String[] from = listDataKey;
    	int[] to = new int[] { R.id.name };
    	SimpleAdapter listItems = new SimpleAdapter(this, listDatas, R.layout.list_item, from, to);
    	
    	setListAdapter(listItems);
    }
    
    private void sayHello(){
    	new AlertDialog.Builder(this)
    	.setTitle(R.string.title_dialog_test)
    	.setPositiveButton(R.string.title_dialog_done, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		})
		.setMessage(R.string.title_dialog_txt).show();
    }
}